home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / PIL / PSDraw.py < prev    next >
Text File  |  2006-12-03  |  6KB  |  200 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id: PSDraw.py 2813 2006-10-07 10:11:35Z fredrik $
  4. #
  5. # simple postscript graphics interface
  6. #
  7. # History:
  8. # 1996-04-20 fl   Created
  9. # 1999-01-10 fl   Added gsave/grestore to image method
  10. # 2005-05-04 fl   Fixed floating point issue in image (from Eric Etheridge)
  11. #
  12. # Copyright (c) 1997-2005 by Secret Labs AB.  All rights reserved.
  13. # Copyright (c) 1996 by Fredrik Lundh.
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17.  
  18. import EpsImagePlugin
  19. import string
  20.  
  21. ##
  22. # Simple Postscript graphics interface.
  23.  
  24. class PSDraw:
  25.  
  26.     def __init__(self, fp=None):
  27.         if not fp:
  28.             import sys
  29.             fp = sys.stdout
  30.         self.fp = fp
  31.  
  32.     def begin_document(self, id = None):
  33.         "Write Postscript DSC header"
  34.         # FIXME: incomplete
  35.         self.fp.write("%!PS-Adobe-3.0\n"
  36.                       "save\n"
  37.                       "/showpage { } def\n"
  38.                       "%%EndComments\n"
  39.                       "%%BeginDocument\n")
  40.         #self.fp.write(ERROR_PS) # debugging!
  41.         self.fp.write(EDROFF_PS)
  42.         self.fp.write(VDI_PS)
  43.         self.fp.write("%%EndProlog\n")
  44.         self.isofont = {}
  45.  
  46.     def end_document(self):
  47.         "Write Postscript DSC footer"
  48.         self.fp.write("%%EndDocument\n"
  49.                       "restore showpage\n"
  50.                       "%%End\n")
  51.         if hasattr(self.fp, "flush"):
  52.             self.fp.flush()
  53.  
  54.     def setfont(self, font, size):
  55.         if not self.isofont.has_key(font):
  56.             # reencode font
  57.             self.fp.write("/PSDraw-%s ISOLatin1Encoding /%s E\n" %\
  58.                           (font, font))
  59.             self.isofont[font] = 1
  60.         # rough
  61.         self.fp.write("/F0 %d /PSDraw-%s F\n" % (size, font))
  62.  
  63.     def setink(self, ink):
  64.         print "*** NOT YET IMPLEMENTED ***"
  65.  
  66.     def line(self, xy0, xy1):
  67.         xy = xy0 + xy1
  68.         self.fp.write("%d %d %d %d Vl\n" % xy)
  69.  
  70.     def rectangle(self, box):
  71.         self.fp.write("%d %d M %d %d 0 Vr\n" % box)
  72.  
  73.     def text(self, xy, text):
  74.         text = string.joinfields(string.splitfields(text, "("), "\\(")
  75.         text = string.joinfields(string.splitfields(text, ")"), "\\)")
  76.         xy = xy + (text,)
  77.         self.fp.write("%d %d M (%s) S\n" % xy)
  78.  
  79.     def image(self, box, im, dpi = None):
  80.         "Write an PIL image"
  81.         # default resolution depends on mode
  82.         if not dpi:
  83.             if im.mode == "1":
  84.                 dpi = 200 # fax
  85.             else:
  86.                 dpi = 100 # greyscale
  87.         # image size (on paper)
  88.         x = float(im.size[0] * 72) / dpi
  89.         y = float(im.size[1] * 72) / dpi
  90.         # max allowed size
  91.         xmax = float(box[2] - box[0])
  92.         ymax = float(box[3] - box[1])
  93.         if x > xmax:
  94.             y = y * xmax / x; x = xmax
  95.         if y > ymax:
  96.             x = x * ymax / y; y = ymax
  97.         dx = (xmax - x) / 2 + box[0]
  98.         dy = (ymax - y) / 2 + box[1]
  99.         self.fp.write("gsave\n%f %f translate\n" % (dx, dy))
  100.         if (x, y) != im.size:
  101.             # EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
  102.             sx = x / im.size[0]
  103.             sy = y / im.size[1]
  104.             self.fp.write("%f %f scale\n" % (sx, sy))
  105.         EpsImagePlugin._save(im, self.fp, None, 0)
  106.         self.fp.write("\ngrestore\n")
  107.  
  108. # --------------------------------------------------------------------
  109. # Postscript driver
  110.  
  111. #
  112. # EDROFF.PS -- Postscript driver for Edroff 2
  113. #
  114. # History:
  115. # 94-01-25 fl: created (edroff 2.04)
  116. #
  117. # Copyright (c) Fredrik Lundh 1994.
  118. #
  119.  
  120. EDROFF_PS = """\
  121. /S { show } bind def
  122. /P { moveto show } bind def
  123. /M { moveto } bind def
  124. /X { 0 rmoveto } bind def
  125. /Y { 0 exch rmoveto } bind def
  126. /E {    findfont
  127.         dup maxlength dict begin
  128.         {
  129.                 1 index /FID ne { def } { pop pop } ifelse
  130.         } forall
  131.         /Encoding exch def
  132.         dup /FontName exch def
  133.         currentdict end definefont pop
  134. } bind def
  135. /F {    findfont exch scalefont dup setfont
  136.         [ exch /setfont cvx ] cvx bind def
  137. } bind def
  138. """
  139.  
  140. #
  141. # VDI.PS -- Postscript driver for VDI meta commands
  142. #
  143. # History:
  144. # 94-01-25 fl: created (edroff 2.04)
  145. #
  146. # Copyright (c) Fredrik Lundh 1994.
  147. #
  148.  
  149. VDI_PS = """\
  150. /Vm { moveto } bind def
  151. /Va { newpath arcn stroke } bind def
  152. /Vl { moveto lineto stroke } bind def
  153. /Vc { newpath 0 360 arc closepath } bind def
  154. /Vr {   exch dup 0 rlineto
  155.         exch dup neg 0 exch rlineto
  156.         exch neg 0 rlineto
  157.         0 exch rlineto
  158.         100 div setgray fill 0 setgray } bind def
  159. /Tm matrix def
  160. /Ve {   Tm currentmatrix pop
  161.         translate scale newpath 0 0 .5 0 360 arc closepath
  162.         Tm setmatrix
  163. } bind def
  164. /Vf { currentgray exch setgray fill setgray } bind def
  165. """
  166.  
  167. #
  168. # ERROR.PS -- Error handler
  169. #
  170. # History:
  171. # 89-11-21 fl: created (pslist 1.10)
  172. #
  173.  
  174. ERROR_PS = """\
  175. /landscape false def
  176. /errorBUF 200 string def
  177. /errorNL { currentpoint 10 sub exch pop 72 exch moveto } def
  178. errordict begin /handleerror {
  179.     initmatrix /Courier findfont 10 scalefont setfont
  180.     newpath 72 720 moveto $error begin /newerror false def
  181.     (PostScript Error) show errorNL errorNL
  182.     (Error: ) show
  183.         /errorname load errorBUF cvs show errorNL errorNL
  184.     (Command: ) show
  185.         /command load dup type /stringtype ne { errorBUF cvs } if show
  186.         errorNL errorNL
  187.     (VMstatus: ) show
  188.         vmstatus errorBUF cvs show ( bytes available, ) show
  189.         errorBUF cvs show ( bytes used at level ) show
  190.         errorBUF cvs show errorNL errorNL
  191.     (Operand stargck: ) show errorNL /ostargck load {
  192.         dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
  193.     } forall errorNL
  194.     (Execution stargck: ) show errorNL /estargck load {
  195.         dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
  196.     } forall
  197.     end showpage
  198. } def end
  199. """
  200.